namespace Spotted.Storage.Tables.ViewModels
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Threading;
    using Microsoft.WindowsAzure.Samples.Phone.Storage;

    public class ListTablesPageViewModel : TableBaseViewModel<TableServiceSchema>
    {
        private readonly ICloudTableClient client;

        public ListTablesPageViewModel()
            : this(CloudStorageContext.Current.Resolver.CreateCloudTableClient(), Deployment.Current.Dispatcher)
        {
        }

        public ListTablesPageViewModel(ICloudTableClient client, Dispatcher dispatcher)
            : base(client.GetDataServiceContext(), dispatcher)
        {
            this.client = client;
        }

        public override string TableName
        {
            get { return "Tables"; }
        }

        public void NewTable()
        {
            this.Message = "Creating new table...";
            this.IsLoading = true;

            try
            {
                var uniqueTableName = string.Concat("Table", DateTime.Now.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture));
                this.client.CreateTable(
                    uniqueTableName,
                    response =>
                    {
                        this.IsLoading = false;

                        if (response.Success)
                        {
                            this.Message = "Table successfully created.";
                            this.Table.Add(new TableServiceSchema(uniqueTableName));
                        }
                        else
                        {
                            this.Message = string.Concat("Error: ", response.ErrorMessage);
                        }
                    });
            }
            catch (Exception exception)
            {
                this.IsLoading = false;
                this.Message = string.Concat("Error: ", exception.Message);
            }
        }

        public void DeleteTable(TableServiceSchema table)
        {
            this.Message = "Deleting table...";
            this.IsLoading = true;

            try
            {
                this.client.DeleteTable(
                    table.TableName,
                    response =>
                    {
                        this.IsLoading = false;

                        if (response.Success)
                        {
                            this.Message = "Table successfully deleted.";
                            this.Table.Remove(table);
                        }
                        else
                        {
                            this.Message = string.Concat("Error: ", response.ErrorMessage);
                        }
                    });
            }
            catch (Exception exception)
            {
                this.IsLoading = false;
                this.Message = string.Concat("Error: ", exception.Message);
            }
        }
    }
}
